package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;


    @Override
    public List<NursingLevelVo> queryAllNursingLevel() {

        List<NursingLevelVo> levelVoList = nursingLevelMapper.queryAllNursingLevel();
        return levelVoList;
    }

    @Override
    public void addNursingLevel(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        Integer count = nursingLevelMapper.selectByName(nursingLevel.getName());
        if(count>0){
            throw new BaseException(BasicEnum.NURSING_LEVEL_EXEIST);
        }
        nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public PageResponse<NursingLevelVo> pageQuery(String name, Integer pageNum, Integer pageSize, Integer status) {

        PageHelper.startPage(pageNum,pageSize);
        Page<NursingLevelVo> paged =nursingLevelMapper.pageQuery(name,status);
        return  PageResponse.of(paged, NursingLevelVo.class);
    }

    @Override
    public NursingLevelVo queryById(Integer id) {


        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);

        return nursingLevelVo;
    }


    @Override
    public void updateNursingLevel(NursingLevelVo vo) {
        NursingLevel nursingLevel = BeanUtil.toBean(vo, NursingLevel.class);
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(Math.toIntExact(nursingLevel.getId()));
        if (nursingLevelVo.getCid()!=null){
            throw new BaseException(BasicEnum.NURSING_LEVEL_UPDATE_FAIL);
        }
        nursingLevelMapper.update(nursingLevel);

    }

    @Override
    public void deleteById(Integer id) {
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);
        if (nursingLevelVo.getCid()!=null){
            throw new BaseException(BasicEnum.NURSING_LEVEL_DELETE_FAIL);
        }
        nursingLevelMapper.delete(id);

    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        NursingLevel nursingLevel=new NursingLevel();
        nursingLevel.setId(Long.valueOf(id));
        nursingLevel.setStatus(status);
        nursingLevelMapper.update(nursingLevel);
    }



}
